<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Object_RelBookUserHandler extends Paylog_Abstract_ObjectHandler
{
	protected $object  = 'Paylog_Object_RelBookUser';
	protected $table   = 'rel_book_user';
	protected $primary = 'id';

	public function isOwner($userId)
	{
		$userId = intval($userId);
		$sql = "SELECT COUNT(`id`) FROM `%s` WHERE `uid` = '%u'";
		$sql = sprintf($sql, $this->table, $userId);
		$result = $this->_query($sql);
		list($total) = $this->db->fetchRow($result);
		return ( $total > 0 );
	}

	public function getBookIdsByUserId($userId)
	{
		$userId = intval($userId);
		$sql = "SELECT `book_id` FROM `%s` WHERE `uid` = '%u'";
		$sql = sprintf($sql, $this->table, $userId);
		$result = $this->_query($sql);

		$bookIds = array();

		while ( list($bookId) = $this->db->fetchRow($result) )
		{
			$bookIds[] = $bookId;
		}

		return $bookIds;
	}

	public function deleteByBookId($bookId)
	{
		$bookId = intval($bookId);
		$sql = "DELETE FROM `%s` WHERE `book_id` = '%u'";
		$sql = sprintf($sql, $this->table, $bookId);
		return $this->_query($sql);
	}
}

?>
